package com.hm.ipay.utils;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.schedule.ScheduleResult;
import jdk.internal.dynalink.linker.LinkerServices;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Jdpush {

    // demo App defined in resources/jpush-api.conf

    public static final String TITLE = "申通快递";
    public static final String ALERT = "祝大家新春快乐";
    public static final String MSG_CONTENT = "申通快递祝新老客户新春快乐";
    public static final String REGISTRATION_ID = "0900e8d85ef";
    public static final String TAG = "tag_api";
    private static final String APP_KEY = "4d23f69c24fbc5dc5767cac8";
    private static final String MASTER_SECRET = "1c5245a13aeb0debc13686e6";

    public static JPushClient jpushClient = null;

    public static void testSendPush(String alert, String title, String id) {
        jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        // HttpProxy proxy = new HttpProxy("localhost", 3128);
        // Can use this https proxy: https://github.com/Exa-Networks/exaproxy

        // For push, all you need do is to build PushPayload object.
        //PushPayload payload = buildPushObject_all_all_alert();
        //生成推送的内容
        PushPayload payload = buildPushObject_android_tag_alertWithTitle(alert, title, id);
        try {
            System.out.println(payload.toString());
            PushResult result = jpushClient.sendPush(payload);
            System.out.println(result + "................................");
        } catch (APIConnectionException e) {

        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    public static void gameLotteryPush(String alert, String title, String id, Collection<String> regs) {
        jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        PushPayload payload = buildPushObject_android_tag_alertWithReg(alert, title, id, regs);
        try {
            PushResult result = jpushClient.sendPush(payload);
        } catch (APIConnectionException e) {

        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //Jdpush.testSendPush("测试123", "ipay title", "1");

//        Set<String> partInUserPush = new HashSet<>();
//        partInUserPush.add("1507bfd3f79894663e4");
//        Jdpush.gameLotteryPush("测试123", "ipay title", "73", partInUserPush);

        String string = "2019-04-28 16:26:00";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Jdpush.sendSchedulePushAll("alert", "title", sdf.parse(string), "name", "id");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public static PushPayload buildPushObject_all_all_alert() {
        return PushPayload.alertAll(ALERT);
    }

    public static PushPayload buildPushObject_all_alias_alert() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.all())//设置接受的平台
                .setAudience(Audience.all())//Audience设置为all，说明采用广播方式推送，所有用户都可以接收到
                .setNotification(Notification.alert(ALERT))
                .build();
    }

    public static PushPayload buildPushObject_android_tag_alertWithReg(String alert, String title, String gameId, Collection<String> regs) {
        Map<String, String> extras = new HashMap<>();
        extras.put("type", "2");
        extras.put("gameId", gameId);
        extras.put("title", title);
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(regs))
                .setNotification(Notification.android(alert, title, extras))
                .build();
    }

    public static PushPayload buildPushObject_android_tag_alertWithTitle(String alert, String title, String id) {
        Map<String, String> extras = new HashMap<>();
        extras.put("id", id);
        extras.put("type", "1");
        extras.put("title", title);
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.android("查看详情", title, extras))
                .build();
    }

    public static PushPayload buildPushObject_android_and_ios() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.tag("tag1"))
                .setNotification(Notification.newBuilder()
                        .setAlert("alert content")
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                .setTitle("Android Title").build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .incrBadge(1)
                                .addExtra("extra_key", "extra_value").build())
                        .build())
                .build();
    }

    public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.ios())
                .setAudience(Audience.tag_and("tag1", "tag_all"))
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(ALERT)
                                .setBadge(5)
                                .setSound("happy")
                                .addExtra("from", "JPush")
                                .build())
                        .build())
                .setMessage(Message.content(MSG_CONTENT))
                .setOptions(Options.newBuilder()
                        .setApnsProduction(true)
                        .build())
                .build();
    }

    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
                        .addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
                        .build())
                .setMessage(Message.newBuilder()
                        .setMsgContent(MSG_CONTENT)
                        .addExtra("from", "JPush")
                        .build())
                .build();
    }

    /**
     *
     * @auth Ren
     * @date 2018年5月2日
     * @decripe 定时推送,推送到所有设备,同时记录返回的msg_id
     */
    public static void sendSchedulePushAll(String alert, String title, Date date, String name, String id) {
        JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
//        System.out.println("time==="+time);
        Map<String, String> extras = new HashMap<>();
        extras.put("id", id);
        extras.put("type", "1");
        extras.put("title", title);
        PushPayload push = PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.android("查看详情", title, extras))
                .build();
        try {
            //System.out.println("date = " + date);
            jPushClient.createSingleSchedule(name, time, push);
        } catch (APIConnectionException e) {

        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    public static void gameSchedulePushAll(String alert, String title, Date date, String name, String id) {
        JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
//        System.out.println("time==="+time);
        Map<String, String> extras = new HashMap<>();
        extras.put("gameId", id);
        extras.put("type", "3");
        extras.put("title", title);
        PushPayload push = PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.all())
                .setNotification(Notification.android(alert, title, extras))
                .build();
        try {
            //System.out.println("date = " + date);
            jPushClient.createSingleSchedule(name, time, push);
        } catch (APIConnectionException e) {

        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

}
